package com.sun.showcase.controller.app.mall;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.utils.StringUtils;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.sun.showcase.biz.mall.service.OrderService;
import com.sun.showcase.biz.mall.service.PayOrderService;
import com.sun.showcase.client.query.mall.OrderQuery;
import com.sun.showcase.client.vo.order.PayOrderMiniAppEntity;
import com.sun.showcase.client.vo.order.SaveOrderEntity;
import com.sun.showcase.client.vo.order.SaveOrderResultEntity;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Result;
import com.sun.showcase.utils.ExecuteResult;
import com.sun.showcase.utils.IPUtils;
@CrossOrigin
@RestController("appOrderController")
@RequestMapping(value="/app/order") 
public class OrderController{
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private PayOrderService payOrderService;
	
	private Result result = new Result();
	/**
	 * 跳转到Order管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="saveOrder")
	public Result saveOrder(@RequestBody SaveOrderEntity info) {
		ExecuteResult<SaveOrderResultEntity> res = orderService.saveOrder(info);
		result.setSuccess(res.isSuccess());
		if(res.isSuccess()) {
			result.setMsg(res.getSuccessMessage());
			result.setData(res.getResult());
		}else {
			result.setMsg(res.getErrorMessages().get(0));
		}
		
		return result;
	}
	/**
	 * 跳转到Order管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value="payOrder")
	public Result payOrder(@RequestBody PayOrderMiniAppEntity entity, HttpServletRequest request) {
		entity.setUserIp(IPUtils.getIpAddr(request));
		String scheme = request.getScheme();//http
		String serverName = request.getServerName();//localhost
		int serverPort = request.getServerPort();//8080
		String contextPath = request.getContextPath();//项目名
		String url = scheme+"://"+serverName;
		if(serverPort != 80 && serverPort != 443) {
			url = url +":"+serverPort;
		}
		url = url +contextPath+"/app/order/wxNotify";
		entity.setNotifyUrl(url);
		ExecuteResult<WxPayMpOrderResult> res = payOrderService.miniAppPay(entity);
		result.setSuccess(res.isSuccess());
		if(res.isSuccess()) {
			result.setMsg(res.getSuccessMessage());
			result.setData(res.getResult());
		}else {
			result.setMsg(res.getErrorMessages().get(0));
		}
		
		return result;
	}
	
	@RequestMapping(value="list")
	public Result list(OrderQuery orderQuery) {
		if(StringUtils.isEmpty(orderQuery.getUserId())) {
			result.setSuccess(false);
			result.setMsg("无效用户");
			return result;
		}
		orderQuery.setOrderColum("create_date");
		DataGrid dg = orderService.datagrid4App(orderQuery);
		result.setSuccess(true);
		result.setData(dg);
		return result;
	}
	
	@RequestMapping(value="cancel")
	public Result cancel(String id) {
		orderService.cancelOrder(id);
		result.setSuccess(true);
		return result;
	}
	@RequestMapping(value="recive")
	public Result recive(String id) {
		Long count = orderService.reciveOrder(id);
		result.setSuccess(count==1);
		return result;
	}
	@RequestMapping(value="wxNotify")
	public String wxNotify(@RequestBody String xmlData) {
		ExecuteResult<String> res = payOrderService.wxNotify(xmlData);
		if(res.isSuccess()) {
			return WxPayNotifyResponse.success("成功");
		}
		return WxPayNotifyResponse.fail("失败");
	}
}
